package me.yiyou.meihua.ui.page.random

import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import me.yiyou.meihua.MyApplication
import me.yiyou.meihua.data.BaGuaImage
import me.yiyou.meihua.data.SaveBean
import me.yiyou.meihua.database.MeiHuaRepository
import me.yiyou.meihua.utils.LogUtil
import me.yiyou.meihua.utils.getHour
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.random.Random



class RandomViewModel : ViewModel() {

    val meiHuaRepository = MyApplication.appContext.meiHuaRepository

    var number1 by mutableStateOf(0)
    var number2 by mutableStateOf(0)
    var dongyao by mutableStateOf(0)

    var state by mutableStateOf(false)

    // 占问事
    private val _inputContent = mutableStateOf("")
    val inputContent: State<String> = _inputContent

    /**
     * 占问事, 输入内容变化监听
     */
    fun onInputContentChange(content: String) {
        _inputContent.value = content
    }

    // 占问时间
    val time = LocalDateTime.now().format("yyyy/MM/dd HH:mm:ss")

    /**
     * 时间格式化
     */
    fun LocalDateTime.format(pattern: String): String =
        this.format(DateTimeFormatter.ofPattern(pattern))
    /**
     * 随机数
     */
    fun randomNumber() {
        state = true
        number1 = Random.nextInt(100) % 8
        number2 = Random.nextInt(100) % 8
        if ((number1 + number2 + getHour()) % 6 == 0){
            dongyao = 6
        }else{
            dongyao = (number1 + number2 + getHour()) % 6
        }
        LogUtil.e("randomNumber: $number1, $number2, $dongyao")
    }

    /**
     * 画卦
     */
    fun paintGua(shang: Int, xia: Int, dong: Int? = null): BaGuaImage {
        LogUtil.e("paintGua: $shang, $xia, $dong")
        var baguaImage: BaGuaImage = BaGuaImage(false, false, false, false, false, false)
        when (shang) {
            1 -> {
                baguaImage.six = true
                baguaImage.five = true
                baguaImage.four = true
            }

            2 -> {
                baguaImage.six = false
                baguaImage.five = true
                baguaImage.four = true
            }

            3 -> {
                baguaImage.six = true
                baguaImage.five = false
                baguaImage.four = true
            }

            4 -> {
                baguaImage.six = false
                baguaImage.five = false
                baguaImage.four = true
            }

            5 -> {
                baguaImage.six = true
                baguaImage.five = true
                baguaImage.four = false
            }

            6 -> {
                baguaImage.six = false
                baguaImage.five = true
                baguaImage.four = false
            }

            7 -> {
                baguaImage.six = true
                baguaImage.five = false
                baguaImage.four = false
            }

            0 -> {
                baguaImage.six = false
                baguaImage.five = false
                baguaImage.four = false
            }
        }
        when (xia) {
            1 -> {
                baguaImage.three = true
                baguaImage.two = true
                baguaImage.one = true
            }

            2 -> {
                baguaImage.three = false
                baguaImage.two = true
                baguaImage.one = true
            }

            3 -> {
                baguaImage.three = true
                baguaImage.two = false
                baguaImage.one = true
            }

            4 -> {
                baguaImage.three = false
                baguaImage.two = false
                baguaImage.one = true
            }

            5 -> {
                baguaImage.three = true
                baguaImage.two = true
                baguaImage.one = false
            }

            6 -> {
                baguaImage.three = false
                baguaImage.two = true
                baguaImage.one = false
            }

            7 -> {
                baguaImage.three = true
                baguaImage.two = false
                baguaImage.one = false
            }

            0 -> {
                baguaImage.three = false
                baguaImage.two = false
                baguaImage.one = false
            }
        }

        return baguaImage
    }

    /**
     * 保存到数据库
     */
    suspend fun insert(saveBean: SaveBean){
        try {
            meiHuaRepository.insert(saveBean)
        }catch (e: Exception){
            LogUtil.e("插入失败: ${e.message}")
        }
    }
}